/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.ekeyman.client.rest.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.ekeyman.client.rest.service.EkeymanService;
import com.ekeyman.utils.DigitalSignature;
import com.ekeymanlib.dto.AppDeviceApiKeys;
import com.ekeymanlib.dto.AppDeviceRequest;
import com.ekeymanlib.dto.EncryptionKeyRequest;
import com.ekeymanlib.dto.EncryptionKeys;

public class EkeymanServiceImpl implements EkeymanService{

    private static final Log logger = LogFactory.getLog(EkeymanServiceImpl.class);

    
    private static final String baseUrl = "http://localhost:8080/EkeymanServiceRest";
    
    private RestTemplate restTemplate;
    
    public EkeymanServiceImpl(){}
    
	@Override
	public EncryptionKeys createEncryptionKeys(String resourceUri, 
			String publicKey, String privateKey,
			int keyBytesLength, int ivBytesLength, int saltBytesLength, 
			int iterationCountMax, String location) {

		logger.debug("createEncryptionKeys...");
		
		EncryptionKeyRequest request = new EncryptionKeyRequest();
		
		request.setResourceUri(resourceUri);
		request.setPublicKey(publicKey);
		request.setKeyBytesLength(keyBytesLength); 
		request.setIvBytesLength(ivBytesLength);
		request.setSaltBytesLength(saltBytesLength);
		request.setIterationCountMax(iterationCountMax);
		request.setLocation(location);
		
		//create computed digital signature from public/private key and data
		String computedDigitalSignature = DigitalSignature.createDigitalSignature(
				resourceUri, publicKey, privateKey, Integer.toString(keyBytesLength), 
				Integer.toString(ivBytesLength), Integer.toString(saltBytesLength), 
				Integer.toString(iterationCountMax), location);
		
		request.setDigitalSignature(computedDigitalSignature);
		
		HttpEntity<EncryptionKeyRequest> entity = new HttpEntity<EncryptionKeyRequest>(request);
		ResponseEntity<EncryptionKeys> response = restTemplate.postForEntity(
				baseUrl + "/create", entity, EncryptionKeys.class);

		EncryptionKeys encryptionKeys = response.getBody();
		
		return encryptionKeys;
	}

	@Override
	public boolean deleteEncryptionKeys(String resourceUri, String publicKey,
			String privateKey, String location) {
		
		logger.debug("deleteEncryptionKeys...");
		
		EncryptionKeyRequest request = new EncryptionKeyRequest();
		
		request.setResourceUri(resourceUri);
		request.setPublicKey(publicKey);
		request.setLocation(location);
		
		//create computed digital signature from public/private key and data
		String computedDigitalSignature = DigitalSignature.createDigitalSignature(
				resourceUri, publicKey, privateKey, location);

		request.setDigitalSignature(computedDigitalSignature);
		
		HttpEntity<EncryptionKeyRequest> entity = new HttpEntity<EncryptionKeyRequest>(request);
		ResponseEntity<Boolean> response = restTemplate.postForEntity(
				baseUrl + "/delete", entity, Boolean.class);

		Boolean b = response.getBody();
		
		return b;
		
	}

	@Override
	public EncryptionKeys getEncryptionKeys(String resourceUri,
			 String publicKey, String privateKey, String location) {

		logger.debug("getEncryptionKeys...");
		
		EncryptionKeyRequest request = new EncryptionKeyRequest();
		
		request.setResourceUri(resourceUri);
		request.setPublicKey(publicKey);
		request.setLocation(location);
		
		//create computed digital signature from public/private key and data
		String computedDigitalSignature = DigitalSignature.createDigitalSignature(
				resourceUri, publicKey, privateKey, location);

		request.setDigitalSignature(computedDigitalSignature);
		
		HttpEntity<EncryptionKeyRequest> entity = new HttpEntity<EncryptionKeyRequest>(request);
		ResponseEntity<EncryptionKeys> response = restTemplate.postForEntity(
				baseUrl + "/get", entity, EncryptionKeys.class);
		
		EncryptionKeys encryptionKeys = response.getBody();
		
		return encryptionKeys;
	}

	@Override
	public AppDeviceApiKeys registerAppDevice(String apiKey,
			String application, String device) {
		
		logger.debug("registerAppDevice...");
		
		AppDeviceRequest request = new AppDeviceRequest();
		
		request.setApiKey(apiKey);
		request.setApplication(application);
		request.setDevice(device);
		
		HttpEntity<AppDeviceRequest> entity = new HttpEntity<AppDeviceRequest>(request);
		ResponseEntity<AppDeviceApiKeys> response = restTemplate.postForEntity(
				baseUrl + "/register", entity, AppDeviceApiKeys.class);
	
		AppDeviceApiKeys appDeviceApiKeys = response.getBody();
	
		return appDeviceApiKeys;	
	}

	public RestTemplate getRestTemplate() {
		return restTemplate;
	}

	public void setRestTemplate(RestTemplate restTemplate) {
		this.restTemplate = restTemplate;
	}

}
